package cn.stylefeng.guns.modular.school.service.impl;

import cn.stylefeng.guns.base.consts.ConstantsContext;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.school.entity.StudentInfo;
import cn.stylefeng.guns.modular.school.mapper.StudentInfoMapper;
import cn.stylefeng.guns.modular.school.model.params.StudentInfoParam;
import cn.stylefeng.guns.modular.school.model.result.StudentInfoResult;
import cn.stylefeng.guns.modular.school.service.StudentInfoService;
import cn.stylefeng.guns.sys.core.constant.Const;
import cn.stylefeng.guns.sys.core.shiro.ShiroKit;
import cn.stylefeng.guns.sys.modular.system.entity.Role;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.model.UserDto;
import cn.stylefeng.guns.sys.modular.system.service.RoleService;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 学生信息 服务实现类
 * </p>
 *
 * @author
 * @since 2019-11-15
 */
@Service
public class StudentInfoServiceImpl extends ServiceImpl<StudentInfoMapper, StudentInfo> implements StudentInfoService {
    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;

    @Override
    @Transactional(rollbackOn = Exception.class)
    public void add(StudentInfoParam param) {
        StudentInfo entity = getEntity(param);
        this.save(entity);

        UserDto userDto = new UserDto();
        userDto.setAccount(param.getCode());
        if (StringUtils.isEmpty(param.getPassword())) {
            String defaultPassword = ConstantsContext.getDefaultPassword();
            userDto.setPassword(defaultPassword);
        } else {
            userDto.setPassword(param.getPassword());
        }
        //userDto.setBirthday(param.getBirthday());
        userDto.setName(param.getName());
        //userDto.setPhone(param.getTel());
        userDto.setSex(param.getSex());
        //Role role = roleService.findByDescription(Const.CLASS_TEACHER);
        //userDto.setRoleId(role.getRoleId().toString());
        userService.addUser(userDto);
    }

    @Override
    public void delete(StudentInfoParam param) {
        this.removeById(getKey(param));
    }

    @Override
    public void update(StudentInfoParam param) {
        // 添加账户
//        String userAccount = param.getGuardianTel();
//        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("account", userAccount);
//        User user = userService.getOne(queryWrapper);
//        if (user == null) {
//            QueryWrapper<Role> roleQueryWrapper = new QueryWrapper<>();
//            roleQueryWrapper.eq("description", "JWT");
//            Role role = roleService.getOne(roleQueryWrapper);
//            String pwd = ConstantsContext.getDefaultPassword();
//            String salt = ShiroKit.getRandomSalt(5);
//            String password = ShiroKit.md5(pwd, salt);
//            user = new User();
//            user.setAccount(param.getGuardianTel());
//            user.setName(param.getGuardianName());
//            user.setPassword(password);
//            user.setSalt(salt);
//            user.setStatus("ENABLE");
//            user.setRoleId(role.getRoleId().toString());
//            user.setDeptId(ShiroKit.getUser().getDeptId());
//            this.userService.save(user);
//        }else{
//
//        }

        StudentInfo oldEntity = getOldEntity(param);
        StudentInfo newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public StudentInfoResult findBySpec(StudentInfoParam param) {
        return null;
    }

    @Override
    public List<StudentInfoResult> findListBySpec(StudentInfoParam param) {
        return this.baseMapper.customList(param);
    }

    @Override
    public LayuiPageInfo findPageBySpec(StudentInfoParam param) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public StudentInfo findByCode(String code) {
        QueryWrapper<StudentInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("code", code);
        return this.getOne(queryWrapper);
    }

    @Override
    public List<StudentInfo> findByClassId(Integer classId) {
        List<StudentInfo> studentInfos = this.list(new QueryWrapper<StudentInfo>().eq("class_id", classId));
        return studentInfos;
    }

    private Serializable getKey(StudentInfoParam param) {
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private StudentInfo getOldEntity(StudentInfoParam param) {
        return this.getById(getKey(param));
    }

    private StudentInfo getEntity(StudentInfoParam param) {
        StudentInfo entity = new StudentInfo();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
